import Vue from 'vue'
import Router from 'vue-router'
import home from '@/view/home'
Vue.use(Router)
// Vue.use(ElementUI)
const router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: home
    },
    {
      path:'/homeContent',
      name:'homeContent',
      component:() => import ('@/view/homeContent.vue')
    },
    {
      path:'/login',
      name:'login',
      component:() => import ('@/view/login.vue')
    },
    {
      path:'/register',
      component:() => import ('@/view/register.vue')
    },
    {
      path:'/test',
      component:() => import ('@/view/test.vue')
    },
    // 法币交易
    {
      path:'/legalTrade',
      name:'legalTrade',
      component:() => import ('@/view/legal/LegalTrade.vue')
    },
    {
      path:'/legalRecord',
      component:() => import ('@/view/legal/legalRecord.vue')
    },
    {
      path:'/LegalPay',
      component:() => import ('@/view/legal/LegalPay.vue')
    },
    {
      path:'/LegalPayDetail',
      component:() => import ('@/view/legal/LegalPayDetail.vue')
    },
    {
      path:'/MyLegalShops',
      component:() => import ('@/view/legal/MyLegalShops.vue')
    },
    {
      path:'/LegalShopDetail',
      component:() => import ('@/view/legal/LegalShopDetail.vue')
    },
    {
      path:'/shopLegalRecord',
      component:() => import ('@/view/legal/shopLegalRecord.vue')
    },
    {
      path:'/shopLegalPayDetail',
      component:() => import ('@/view/legal/shopLegalPayDetail.vue')
    },
    // c2c交易
    {
      path:'/c2c',
      component:() => import ('@/view/c2c/c2c.vue')
    },
    {
      path:'/c2cRelease',
      name:'c2cRelease',
      component:() => import ('@/view/c2c/c2cRelease.vue')
    },
    {
      path:'/c2cTrade',
      name:'c2cTrade',
      component:() => import ('@/view/c2c/c2cTrade.vue')
    },
    {
      path:'/c2cOrder',
      name:'c2cOrder',
      component:() => import ('@/view/c2c/c2cOrder.vue')
    },
    {
      path:'/c2cDetail',
      name:'c2cDetail',
      component:() => import ('@/view/c2c/c2cDetail.vue')
    },

  ]
});
router.beforeEach((to,from,next) => {
      // console.log(to,from)
      const nextRoute = ['home', 'homeContent'];
      let isLogin = true;  // 是否登录
      // 未登录状态；当路由到nextRoute指定页时，跳转至login
      if (nextRoute.indexOf(to.name) >= 0) {  
        if (!isLogin) {
          // this.$message('登录超时，请重新登录');
          Vue.prototype.$message({
            message:'请登录后操作'
          });
          setTimeout(function(){
            // router.push({ name: 'login' })
            router.replace({  
              path: '/login',  
              query: {redirect: router.currentRoute.fullPath}//登录成功后跳入浏览的当前页面  
            }) 
          },1000)
        }
      }
      // 已登录状态；当路由到login时，跳转至home 
      // if (to.name === 'login') {
      //   if (isLogin) {
      //     router.push({ name: 'home' });
      //   }
      // }
      next();
    })
export default router;
